package qualomelhor.game;

import java.util.Date;

import react.Slot;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;

import playn.core.AssetWatcher;
import playn.core.Font;
import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.PlayN;
import static playn.core.PlayN.assets;
import static playn.core.PlayN.graphics;
import tripleplay.game.ScreenStack;
import tripleplay.game.UIAnimScreen;
import tripleplay.ui.Background;
import tripleplay.ui.Button;
import tripleplay.ui.Group;
import tripleplay.ui.Root;
import tripleplay.ui.SimpleStyles;
import tripleplay.ui.Style;
import tripleplay.ui.Styles;
import tripleplay.ui.Stylesheet;
import tripleplay.ui.layout.AxisLayout;

public class TrocaAvatar extends UIAnimScreen {

	public static final Font TITLE_FONT = graphics().createFont("Helvetica", Font.Style.PLAIN, 12);
    protected final ScreenStack _stack;
    protected Root _root;
    public Button back;
    public String text = "Ola!";
    //public Label lb;
    public Button btnTrocaAvatar;
    public Button btnTrocaCabelo;
    public Button btnTrocaSobrancelha;
    public Button btnTrocaOlho;
    public Button btnTrocaRoupa;
    public Button btnTrocaBoca;
    //public tripleplay.ui.Field field;
    public Timer timerShow;
    public GroupLayer gLayer;
    public boolean hideOnTime = false;
    public boolean isVisible = false;
	private final int menorTempoFade = 30;
	private final int maiorTempoFade = 200;
	private Date timeUltimaMessagem;
	private int posX = 60;
	private int posY = 5;

	public static native void trocaAvatar() /*-{
		$wnd.trocaAvatarAleatorio_j(true);
	}-*/;

	public static native void trocaCabelo() /*-{
		$wnd.trocaProximaParteAvatar_j("cabelo");
	}-*/;

	public static native void trocaSobrancelha() /*-{
		$wnd.trocaProximaParteAvatar_j("sobrancelha");
	}-*/;

	public static native void trocaOlho() /*-{
		$wnd.trocaProximaParteAvatar_j("olho");
	}-*/;

	public static native void trocaRoupa() /*-{
		$wnd.trocaProximaParteAvatar_j("roupa");
	}-*/;

	public static native void trocaBoca() /*-{
		$wnd.trocaProximaParteAvatar_j("boca");
	}-*/;

    public TrocaAvatar(ScreenStack stack, GroupLayer layer) {
    	_stack = stack;
    	gLayer = layer;
    	timeUltimaMessagem = new Date();
    }

    @Override public void wasAdded () {
        super.wasAdded();
        _root = iface.createRoot(AxisLayout.vertical().gap(0).offStretch(), stylesheet(), layer);
        _root.addStyles(Style.BACKGROUND.is(background()), Style.VALIGN.top);

        //_root.setSize(width(), height());
        //_root.setSize(100, 100);
        _root.setBounds(posX, posY, 280, 60);
        //_root.setVisible(false);
        
        //Background bg = Background.solid(0xFFCC99FF).inset(0, 0, 5, 0);
        /*
        _root.add(new Group(AxisLayout.horizontal(), Style.HALIGN.left, Style.BACKGROUND.is(bg)).add(
                      //this.back = new Button("Back"),
                      new Label(title()).addStyles(Style.FONT.is(TITLE_FONT), Style.HALIGN.center).
                      setConstraint(AxisLayout.stretched())));
                      
        if (subtitle() != null) _root.add(new Label(subtitle()));
        */
        Group iface = createIface();
        if (iface != null) _root.add(iface.setConstraint(AxisLayout.fixed()));
    }

    @Override public void wasRemoved () {
        super.wasRemoved();
        iface.destroyRoot(_root);
        while (layer.size() > 0) layer.get(0).destroy();
    }

    protected String name () {
        return "Label Test";
    }

    protected String title () {
        return "UI: Labels Test";
    }
    /** Returns an explanatory subtitle for this demo, or null. */
    protected String subtitle () { return null; }

    /** Override this method and return a group that contains your main UI, or null. */
    protected Group createIface () {
        Styles bigLabel = Styles.make(
            Style.FONT.is(PlayN.graphics().createFont("Times New Roman", Font.Style.BOLD, 18)),
            Style.VALIGN.center);

	    final Image imgIconTudo = assets().getImage("/images/icons/icone_tudo.png");
	    final Image imgIconCabelo = assets().getImage("/images/icons/icone_cabelo.png");
	    final Image imgIconSobran = assets().getImage("/images/icons/icone_sobran.png");
	    final Image imgIconOlhos = assets().getImage("/images/icons/icone_olhos.png");
	    final Image imgIconRoupa = assets().getImage("/images/icons/icone_roupa.png");
	    final Image imgIconBoca = assets().getImage("/images/icons/icone_boca.png");

	    AssetWatcher as = new AssetWatcher(new AssetWatcher.Listener() {
			@Override
			public void error(Throwable e) {
			}

			@Override
			public void done() {
		        btnTrocaAvatar.icon.update(imgIconTudo);
		        btnTrocaCabelo.icon.update(imgIconCabelo);
		        btnTrocaSobrancelha.icon.update(imgIconSobran);
		        btnTrocaOlho.icon.update(imgIconOlhos);
		        btnTrocaRoupa.icon.update(imgIconRoupa);
		        btnTrocaBoca.icon.update(imgIconBoca);
			}
		});
	    as.add(imgIconTudo);
	    as.add(imgIconCabelo);
	    as.add(imgIconSobran);
	    as.add(imgIconOlhos);
	    as.add(imgIconRoupa);
	    as.add(imgIconBoca);

        btnTrocaAvatar = new Button();
        btnTrocaAvatar.setVisible(false);
        btnTrocaAvatar.clicked().connect(new Slot<Button>() {
            public void onEmit(Button b) {
            	trocaAvatar();
            }
        });

        btnTrocaCabelo = new Button();
        btnTrocaCabelo.setVisible(false);
        btnTrocaCabelo.clicked().connect(new Slot<Button>() {
            public void onEmit(Button b) {
            	trocaCabelo();
            }
        });

        btnTrocaSobrancelha = new Button();
        btnTrocaSobrancelha.setVisible(false);
        btnTrocaSobrancelha.clicked().connect(new Slot<Button>() {
            public void onEmit(Button b) {
            	trocaSobrancelha();
            }
        });

        btnTrocaOlho = new Button();
        btnTrocaOlho.setVisible(false);
        btnTrocaOlho.clicked().connect(new Slot<Button>() {
            public void onEmit(Button b) {
            	trocaOlho();
            }
        });

        btnTrocaRoupa = new Button();
        btnTrocaRoupa.setVisible(false);
        btnTrocaRoupa.clicked().connect(new Slot<Button>() {
            public void onEmit(Button b) {
            	trocaRoupa();
            }
        });

        btnTrocaBoca = new Button();
        btnTrocaBoca.setVisible(false);
        btnTrocaBoca.clicked().connect(new Slot<Button>() {
            public void onEmit(Button b) {
            	trocaBoca();
            }
        });

        Background bgTransp = Background.solid(0xFFCC99FF);
        Group textGroup = new Group(AxisLayout.horizontal().gap(5)).add(
            					btnTrocaAvatar.addStyles(bigLabel),
            					btnTrocaCabelo.addStyles(bigLabel),
            					btnTrocaRoupa.addStyles(bigLabel),
            					btnTrocaSobrancelha.addStyles(bigLabel),
            					btnTrocaOlho.addStyles(bigLabel),
            					btnTrocaBoca.addStyles(bigLabel)
            					
        	);
  
        //btnTrocaAvatar.bounds(new Rectangle(0, 0, 30, 30));
        //btnTrocaRoupa.bounds(new Rectangle(0, 60, 30, 30));

        as.start();
        return textGroup;
    }

    /** Returns the stylesheet to use for this screen. */
    protected Stylesheet stylesheet () {
        return SimpleStyles.newSheet();
    }

    /** Returns the background to use for this screen. */
    protected Background background () {
    	return Background.blank();
        //return Background.bordered(0xFFFFFFFF, 0xFFFFFFFF, 5).inset(10);
    }

    protected float updateRate () {
        return 16;
    }

    public int getPosX() {
    	return posX;
    }

    public int getPosY() {
    	return posY;
    }

    public void setPos(int x, int y) {
    	posX = x;
    	posY = y;
    	_root.setBounds(posX, posY, 280, 60);
    }

    public boolean getVisible() {
    	return isVisible;
    }

    public void setVisible(boolean visible) {
    	isVisible = visible;
    	if (_root != null && _root.layer != null) {
    		//Window.alert("setando trocaavatar:" + visible);
    		_root.layer.setVisible(visible);
    		btnTrocaAvatar.setVisible(visible);
    		btnTrocaBoca.setVisible(visible);
    		btnTrocaCabelo.setVisible(visible);
    		btnTrocaOlho.setVisible(visible);
    		btnTrocaRoupa.setVisible(visible);
    		btnTrocaSobrancelha.setVisible(visible);
    	} else {
    		Window.alert("nao foi possivel setar trocaavatar:" + visible + ", root ou root.layer nulo");
    	}
    }
}